<?php

namespace app\mobile\controller;
use think\Controller;
use think\Request;
use think\Session;
use think\Db;
// use Vendor\ThinkImage\ThinkImage;  
require(ROOT_PATH.'/vendor/topthink/think-image/src/Image.php');

class User extends Controller
{
    public function index()
    {
        $str = Session::get();
//        var_dump($str);die;
        if(!empty($str)){
            $data = db('h_user')
                ->field('name, phone, createtime, email, sex, face, id')
                ->where('status', '1')
                ->where('id',$str['id'])
                ->find();
            $list = Db::table('sm_h_user')
                    ->alias('hu')
                    ->join('sm_h_favorite hf', 'hf.uid = hu.id')
                    ->join('sm_h_movie hm', 'hm.id = hf.mid')
                    ->field('hm.mname, hm.cover, hm.id')
                    ->where('hu.id',$str['id'])
                    ->select();
            $result = Db::table('sm_h_user')
                ->alias('hu')
                ->join('sm_h_comment hc', 'hc.uid = hu.id')
                ->join('sm_h_movie hm', 'hm.id = hc.mid')
                ->field('hc.title, hc.comment, hc.time, hm.id, hm.cover')
                ->where('hu.id', $str['id'])
                ->select();

//            var_dump($result);die;
            $sc = count($list);
            $pp = count($result);
//            die;
//            var_dump($data);die;
            return view('mobile@User/index', [
                'data'=>$data,
                'list'=>$list,
                'sc'=>$sc,
                'pp'=>$pp,
                'result'=>$result
            ]);
        }
        return $this->redirect('mobile/Index/index');
    }

    /**
     * @return \think\response\View|void
     */
    public function edit(Request $request)
    {
        $str = Session::get();
//        $id = $request->get();
//        var_dump($str);die;
        if(!empty($str)){
            $data = db('h_user')
                ->field('name, phone, createtime, email, sex, face, id')
                ->where('id',$str['id'])
                ->find();
            $list = Db::table('sm_h_user')
                ->alias('hu')
                ->join('sm_h_favorite hf', 'hf.uid = hu.id')
                ->join('sm_h_movie hm', 'hm.id = hf.mid')
                ->field('hm.mname, hm.cover, hm.id')
                ->where('hu.id',$str['id'])
                ->select();
            $result = Db::table('sm_h_user')
                ->alias('hu')
                ->join('sm_h_comment hc', 'hc.uid = hu.id')
                ->join('sm_h_movie hm', 'hm.id = hc.mid')
                ->field('hc.title, hc.comment, hc.time, hm.id, hm.cover')
                ->where('hu.id',$str['id'])
                ->select();
//            var_dump($result);die;
            $sc = count($list);
            $pp = count($result);
//            var_dump($list);die;
            return view('mobile@User/edit', [
                'sc'=>$sc,
                'pp'=>$pp,
                'data'=>$data
            ]);
        }
        return $this->redirect('mobile/Index/index');
    }


        public function imgPath(Request $request){
        $data = $request->post();
        // dump($data['uid']);
        // dump(request()->file());exit;
        $file = request()->file('file');
        if ($file != null) {
            $info = $file->move(ROOT_PATH . 'public/uploads');
            if ($info) {
                $a = $info->getSaveName();
                $face = str_replace("\\", "/", $a);
                $user = [
                    'face'=>$face
                ];
                $result = db('h_user')
//                    ->fetchSql()
                    ->where('id', $data['uid'])
                    ->update($user);

                if ($result) {
                    return json($user);
                } else {
                    return false;
                }
            } else {
                echo $file->getError();
            }
        }
    }

//
    public function update(Request $request)
    {
        $data = $request->post();
        $id = Session::get('id');
        $user = [
            'name'=>$data['name'],
            'sex'=>$data['sex'],
        ];
        $result = db('h_user')
            ->where('id', $id)
            ->update($user);
        // dump($result);die;
        if ($result) {
            Session::set('name', $user['name']);
            return $result;
        } else {
            return false;
        }
    }

    public function passEd()
    {
        $id = input('id');
        $data = db('h_user')
            ->field('name, phone, createtime, email, sex, face, id')
            ->where('id',$id)
            ->find();
        $list = Db::table('sm_h_user')
            ->alias('hu')
            ->join('sm_h_favorite hf', 'hf.uid = hu.id')
            ->join('sm_h_movie hm', 'hm.id = hf.mid')
            ->field('hm.mname, hm.cover, hm.id')
            ->where('hu.id',$id)
            ->select();
        $result = Db::table('sm_h_user')
            ->alias('hu')
            ->join('sm_h_comment hc', 'hc.uid = hu.id')
            ->join('sm_h_movie hm', 'hm.id = hc.mid')
            ->field('hc.title, hc.comment, hc.time, hm.id, hm.cover')
            ->where('hu.id',$id)
            ->select();
//            var_dump($result);die;
        $sc = count($list);
        $pp = count($result);
        return view('mobile@User/passedit', [
            'data'=>$data,
            'sc'=>$sc,
            'pp'=>$pp,
            'id'=>$id
        ]);
    }

    public function passUpdate(Request $request)
    {
        $data = $request->post();
        dump($data);die;
        if($data['password'] == $data['repassword']){
            unset($data['repassword']);
            $res = [
                'password'=>md5($data['password'])
            ];
            $str = db('h_user')->where('id', $data['id'])->update($res);
            return $str;
        }else{
            return false;
        }
    }

    public function comment()
    {
        $id = input('id');
        $data = db('h_user')
            ->field('name, phone, createtime, email, sex, face, id')
            ->where('id',$id)
            ->find();
        $list = Db::table('sm_h_user')
            ->alias('hu')
            ->join('sm_h_favorite hf', 'hf.uid = hu.id')
            ->join('sm_h_movie hm', 'hm.id = hf.mid')
            ->field('hm.mname, hm.cover, hm.id, hm.score')
            ->where('hu.id',$id)
            ->select();
        $result = Db::table('sm_h_user')
            ->alias('hu')
            ->join('sm_h_comment hc', 'hc.uid = hu.id')
            ->join('sm_h_movie hm', 'hm.id = hc.mid')
            ->field('hc.title, hc.comment, hc.time, hm.id, hm.cover,hm.mname,hc.id as hcid')
            ->where('hu.id',$id)
            ->select();
//            var_dump($result);die;
        $sc = count($list);
        $pp = count($result);
        return view('mobile@User/comment', [
            'data'=>$data,
            'list'=>$list,
            'result'=>$result,
            'sc'=>$sc,
            'pp'=>$pp,
            'id'=>$id
        ]);
    }

    public function collect()
    {
        $id = input('id');
        $data = db('h_user')
            ->field('name, phone, createtime, email, sex, face, id')
            ->where('id',$id)
            ->find();
        $list = Db::table('sm_h_user')
            ->alias('hu')
            ->join('sm_h_favorite hf', 'hf.uid = hu.id')
            ->join('sm_h_movie hm', 'hm.id = hf.mid')
            ->field('hm.mname, hm.cover, hm.id, hm.score')
            ->where('hu.id',$id)
            ->select();
        $result = Db::table('sm_h_user')
            ->alias('hu')
            ->join('sm_h_comment hc', 'hc.uid = hu.id')
            ->join('sm_h_movie hm', 'hm.id = hc.mid')
            ->field('hc.title, hc.comment, hc.time, hm.id, hm.cover')
            ->where('hu.id',$id)
            ->select();
//            var_dump($result);die;
        $sc = count($list);
        $pp = count($result);
        return view('mobile@User/collect', [
            'data'=>$data,
            'list'=>$list,
            'result'=>$result,
            'sc'=>$sc,
            'pp'=>$pp,
            'id'=>$id
        ]);
    }

    public function delete($id)
    {
        // echo $id;
       $result = db('h_comment')->delete($id);
       if ($result > 0) {
            $info['status'] = true;
            $info['id'] = $id;
        } else {
            $info['status'] = false;
            $info['id'] = $id;
        }
        return json($info);
    }
}